import time

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

from default_config import TYPE_PRICE, TYPE_ORIGIN_PRICE


def crawl(url_lists):
    for urlList in url_lists:
        if "detail.tmall.com" in urlList.url:
            crawl_tmall(urlList)
        elif "item.jd.com" in urlList.url:
            crawl_jd(urlList)


def crawl_tmall(url_list):
    url = url_list.url
    url_list.type = TYPE_PRICE
    # proxy = "127.0.0.1:1080"
    # service_args = ['--proxy=' + proxy, '--proxy-type=socks5']
    driver = webdriver.PhantomJS()
    try:
        driver.get(url)
        try:
            price = driver.find_element_by_xpath('//*[@id="J_PromoPrice"]/dd/div/span').text  # 促销价格
        except NoSuchElementException:
            price = driver.find_element_by_xpath('//*[@id="J_StrPriceModBox"]/dd/span').text  # 正常价格
        url_list.product_price = price
        sellCount = int(
            driver.find_element_by_xpath('//*[@id="J_DetailMeta"]/div[1]/div[1]/div/ul/li[1]/div/span[2]').text) + 1
        url_list.product_sales = sellCount
        productName = driver.find_element_by_xpath('//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[1]/h1').text
        url_list.product_name = productName
        print(productName + "|" + price + "|" + str(sellCount))
        url_list.update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        url_list.is_valid = True
    except Exception as e:
        print(url + ":抓取出错！")
        print(e)
        url_list.is_valid = False
    finally:
        driver.close()
        from run import session
        session.add(url_list)
        try:
            session.commit()
        except:
            session.rollback()


def crawl_jd(url_list):
    url = url_list.url
    # proxy = "127.0.0.1:1080"
    url_list.type = TYPE_PRICE
    # service_args = ['--proxy=' + proxy, '--proxy-type=socks5']
    driver = webdriver.PhantomJS()
    try:
        driver.get(url)
        try:
            price = driver.find_element_by_xpath(
                '/html/body/div[7]/div/div[2]/div[3]/div/div[1]/div[2]/span/span[2]').text
        except NoSuchElementException:
            price = driver.find_element_by_xpath(
                '/html/body/div[5]/div/div[2]/div[4]/div/div[1]/div[2]/span/span[2]').text
        url_list.product_price = price
        sellCount = 1
        url_list.product_sales = sellCount
        try:
            productName = driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div[1]').text
        except NoSuchElementException:
            productName = driver.find_element_by_xpath('/html/body/div[7]/div/div[2]/div[1]').text
        url_list.product_name = productName
        print(productName + "|" + price + "|" + str(sellCount))
        url_list.update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        url_list.is_valid = True
    except Exception as e:
        print(url + ":抓取出错！")
        print(e)
        url_list.is_valid = False
    finally:
        driver.close()
        from run import session
        session.add(url_list)
        try:
            session.commit()
        except:
            session.rollback()


def crawl_1688(url_list):
    url = url_list.url
    # proxy = "127.0.0.1:1080"
    url_list.type = TYPE_ORIGIN_PRICE
    # service_args = ['--proxy=' + proxy, '--proxy-type=socks5']
    driver = webdriver.PhantomJS()
    try:
        driver.get(url)
        price = driver.find_element_by_xpath(
            '//*[@id="mod-detail-price"]/div/table/tbody/tr[1]/td[3]/span[2]').text
        url_list.product_price = price
        sellCount = int(
            driver.find_element_by_xpath('//*[@id="mod-detail-bd"]/div[2]/div[9]/div/div/div[2]/p[2]/a/em').text) + 1
        url_list.product_sales = sellCount
        productName = driver.find_element_by_xpath('//*[@id="mod-detail-title"]/h1').text
        url_list.product_name = productName
        url_list.is_valid = True
        print(productName + "|" + price + "|" + str(sellCount))
        url_list.update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    except Exception as e:
        print(url + ":抓取出错！")
        print(e)
        url_list.is_valid = False
    finally:
        driver.close()
        from run import session
        session.add(url_list)
        try:
            session.commit()
        except:
            session.rollback()
